<%@page import="edu.ncsu.csc.itrust.dao.DAOFactory"%>
<%@page import="edu.ncsu.csc.itrust.action.PatientReferralAction"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PatientReferralDAO"%>
<%@page import="edu.ncsu.csc.itrust.beans.PersonnelBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.PatientReferralBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.OfficeVisitBean"%>
<%@page import="edu.ncsu.csc.itrust.beans.MessageBean"%>
<%@page import="edu.ncsu.csc.itrust.action.SendMessageAction"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO"%>
<%@page import="edu.ncsu.csc.itrust.dao.mysql.OfficeVisitDAO"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>

<%@include file="/global.jsp"%>

<%
	pageTitle = "iTrust - My Referrals";
%>

<%@include file="/header.jsp"%>

<script type="text/javascript">
	function Appt(){
		mainForm.submit();	
	}
</script>

<%
session.removeAttribute("personnelList");
String referral = request.getParameter("refID");
String appointment = request.getParameter("makeAppointment");
PatientReferralDAO patRefDAO = DAOFactory.getProductionInstance().getPatientReferralsDAO();
OfficeVisitDAO ovDAO = DAOFactory.getProductionInstance().getOfficeVisitDAO();
PersonnelDAO personnelDAO = DAOFactory.getProductionInstance().getPersonnelDAO();
SendMessageAction messAction = new SendMessageAction(prodDAO, loggedInMID.longValue());
PatientReferralAction action = new PatientReferralAction(prodDAO, loggedInMID.longValue());
List<PatientReferralBean> patReferral = action.getReferralsForPatient(loggedInMID.longValue());

//Sorts messages
if(request.getParameter("sort") != null) {
	patReferral = action.sort(request.getParameter("sortby"),request.getParameter("sorthow"),loggedInMID+"");
}
%>
<form method="post" action="viewReferrals.jsp">
<div align="center">
<table>
	<tr>
		<td><select name="sortby">
			<option value="hcp">Sort by</option>
			<option value="hcp">Receiving HCP Name</option>
			<option value="time">Time</option>
			<option value="priority">Priority</option>
		</select></td>
		<td><select name="sorthow">
			<option value="desc">Order by</option>
			<option value="asce">Ascending</option>
			<option value="desc">Descending</option>
		</select></td>
		<td><input type="submit" name="sort" value="Sort" /></td>
	</tr>
</table>
</div>
</form>
<br />

<div align="center">
<h2>My Referrals</h2>
<table class="sortable">
	<thead>
		<tr>
			<th align=center>Send To</th>
			<th align=center>Time Generated</th>
			<th align=center>Priority</th>
		</tr>
	</thead>
	<tbody>
		<%	
	for(PatientReferralBean p : patReferral) { %>
		<tr>
			<td><a
				href="myReferrals.jsp?refID=<%= StringEscapeUtils.escapeHtml("" + (p.getId())) %>"><%= StringEscapeUtils.escapeHtml("" + (p.getReceiverID())) %></a></td>
			<td><%= StringEscapeUtils.escapeHtml("" + (p.getDateAdded().toString())) %></td>
			<td><%= StringEscapeUtils.escapeHtml("" + (p.getPriority())) %></td>
		</tr>
		<%} %>
	</tbody>
</table>
</div>
<br />

<%
if (referral != null && !referral.equals("")) {
	//List<HCPDiagnosisBean> hcps = action.getHCPByDiagnosis(icdcode); 
	//loggingAction.logEvent(TransactionType.EXPERIENCED_LHCP_FIND, loggedInMID.longValue(), 0, "Find LHCPs with experience with a diagnosis");
	PatientReferralBean bean = patRefDAO.getPatientReferral(Integer.parseInt(request.getParameter("refID")));
	PersonnelBean sender = personnelDAO.getPersonnel(bean.getSenderID());
	PersonnelBean receiver = personnelDAO.getPersonnel(bean.getReceiverID());
	OfficeVisitBean ov = ovDAO.getOfficeVisit(bean.getOfficeVisitID());
	if(request.getParameter("Appt") != null && !request.getParameter("Appt").equals("")){
		System.out.println("Got here");
		MessageBean message = new MessageBean();
		message.setTo(bean.getReceiverID());
		message.setFrom(bean.getPatientID());
		message.setSubject("Make An Appointment");
		message.setRead(0);
		message.setBody("I wish to make an Appointment, after considering this Referral from " + sender.getFullName());
		messAction.sendMessage(message);
	}
%>
<div align="center">
<h2>Complete Referral Details</h2>
<form
	action="myReferrals.jsp?refID=<%= StringEscapeUtils.escapeHtml("" + bean.getId())%>"
	id="mainForm" method="post"><input type=hidden name="Appt"
	value="Appt">
<table class="fTable">
	<tr>
		<th colspan="9">Complete Referral Details</th>
	</tr>
	<tr>
		<th>Sending HCP</th>
		<th>Sending HCP Specialty</th>
		<th>Receiving HCP</th>
		<th>Receiving HCP Specialty</th>
		<th>Time Generated</th>
		<th>Priority</th>
		<th>Office Visit Date</th>
		<th>Notes</th>
		<th>Action</th>
	</tr>

	<tr>
		<td><%= StringEscapeUtils.escapeHtml("" + (sender.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (sender.getSpecialty())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (receiver.getFullName())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (receiver.getSpecialty())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getDateAdded().toString())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getPriority())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (ov.getVisitDateStr())) %></td>
		<td><%= StringEscapeUtils.escapeHtml("" + (bean.getReferralDetails())) %></td>
		<td><input type="submit" id="makeAppointment"
			value="Make Appointment"></td>
	</tr>

</table>

</form>

</div>
<%
}
%>

<%@include file="/footer.jsp"%>
